#!/usr/bin/env node


let path = process.argv[2];
console.log(path);

const fs = require('fs');
let postMan = JSON.parse(fs.readFileSync(path, "utf-8"));
console.log("加载postman配置文件成功");

let rap = {
    "moduleList": [{
        "name": "某模块（点击编辑后双击修改）",
        "id": 0,
        "pageList": [],
        "introduction": ""
    }
    ],
    "introduction": ""
};

rap.moduleList[0].name = postMan.info.name;
rap.moduleList[0].introduction = postMan.info.description;


let items = {
    "name": "默认",
    "description": "未分类",
    item: []
};

for (let item of postMan.item) {
    if ('request' in item) {
        items.item.push(item);
    }
}
postMan.item.push(items);

for (let item of postMan.item) {
    // 一级
    if ('request' in item) {
        continue;
    }
    //  文件夹--- >page
    let page = {
        "id": 0,
        "name": "",
        "introduction": "",
        actionList: []
    };
    page.name = item.name;
    page.description = item.description;

    for (let request  of    item.item) {
        //请求项目
        let action = {
            "responseParameterList": [],
            "responseTemplate": "",
            "requestParameterList": [],
            "requestType": "2",
            "requestUrl": "",
            "name": "",
            "description": "",
            "id": 762
        };
        action.name = request.name;
        if (typeof (request.request.url) === 'object') {
            action.requestUrl = request.request.url.raw.replace(/\{\{.+?\}\}/g, '');
        } else {
            action.requestUrl = request.request.url.replace(/\{\{.+?\}\}/g, '');
        }
        //删除问号
        if (action.requestUrl.indexOf('?') >= 0) {
            action.requestUrl = action.requestUrl.split("?")[0];
        }
        let method = 1;
        switch (request.request.method) {
            // 1.GET	2.POST	3.PUT	4.DELETE
            case "GET":
                method = 1;
                break;
            case "POST":
                method = 2;
                break;
            case "PUT":
                method = 3;
                break;
            case "DELETE":
                method = 4;
                break;
        }
        action.requestType = method;

        // 1. get
        if (typeof (request.request.url) === 'object') {
            for (let rp  of request.request.url.query) {
                let request = {
                    "identifier": "userId",
                    "dataType": "string",
                    "name": "发布者",
                    "validator": "",
                    "parameterList": [],
                    "remark": "",
                    "id": 0
                };
                request.identifier = rp.key;
                request.name = rp.description;
                action.requestParameterList.push(request)
            }
        }

        //2. body
        if (request.request.body.mode === 'raw') {
            let requestParams = JSON.parse(request.request.body.raw);
            for (let tmp in requestParams) {
                let request = {
                    "identifier": "",
                    "dataType": "string",
                    "name": "",
                    "validator": "",
                    "parameterList": [],
                    "remark": "",
                    "id": 0
                };
                request.identifier = tmp;
                request.name = tmp;
                action.requestParameterList.push(request)
            }
        }

        // 3.urlencoded
        // 请求参数
        if (request.request.body.mode === 'urlencoded') {
            for (let rp  of request.request.body.urlencoded) {
                let request = {
                    "identifier": "userId",
                    "dataType": "string",
                    "name": "发布者",
                    "validator": "",
                    "parameterList": [],
                    "remark": "",
                    "id": 13083
                };
                request.identifier = rp.key;
                request.name = rp.description;
                action.requestParameterList.push(request)
            }
        }


        page.actionList.push(action);
    }
    rap.moduleList[0].pageList.push(page);
}

let b = {"modelJSON": JSON.stringify(rap)};

let savePath = path.replace('postman_collection.', '');
fs.writeFileSync(savePath, JSON.stringify(b), "utf-8");
console.log("导出成功");
console.error(savePath);
